﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TecBaseObjects.Interfaces;
using TecBaseObjects.Objects;
using TecContracts;
using TecData.Manager;
using TpGlobalForms.Forms;
using TpGlobalForms.Interfaces;

namespace TpGlobalForms.Controller {
    
    class ChangeUserController : IController {
    
        #region Members

        private ChangeUserForm myForm;

        #endregion

        #region Constructor

        public ChangeUserController(ITecForm form) {
            myForm = form as ChangeUserForm;
            this.myForm.UserName = Contracts.MyUser.UserName;
            this.myForm.LoginTime = Contracts.MyUser.LoginTime;
        }

        #endregion

        #region Methods

        public void ChangeUser(object sender, EventArgs e) {
            ComboBox cmb = sender as ComboBox;
            int userID = (int)cmb.SelectedValue;
            TecUserObj u = TecUserDataManager.LoadTecUser(userID);
            Contracts.SetMyUser(u);
            TecUserDataManager.SaveUserLogin(u.UserID, u.LoginTime, u.ComputerName);
            this.myForm.UserName = u.UserName;
            this.myForm.LoginTime = u.LoginTime;
        }

        public void CloseForm(object sender, EventArgs e) {
            myForm.Close();
        }

        public void Dispose() {

        }

        public void LoadModel() {
            this.myForm.MyModel = TecUserDataManager.LoadTecUser(false);
        }

        public void Save(IBaseObject myObject) {
            throw new NotImplementedException("ChangeUserController.Save");
        }

        #endregion

        #region Functions

        public string GetCurrentUserName {
            get {
                return Contracts.MyUser.UserName.Trim();
            }
        }

        #endregion

    }
}
